<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
	<% String path=request.getContextPath(); String basePath=request.getScheme() + "://" + request.getServerName() + ":"
		+ request.getServerPort() + path + "/" ; %>

		<!DOCTYPE html>
		<html>

		<head>
			<base href="<%=basePath%>">

			<title>机构管理</title>

			<meta http-equiv="pragma" content="no-cache">
			<meta http-equiv="cache-control" content="no-cache">
			<meta http-equiv="expires" content="0">
			<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
			<meta http-equiv="description" content="This is my page">
			<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

		</head>

		<body>
			<%@ include file="/WEB-INF/jsp/common/header.jsp" %>
				<%@ include file="/WEB-INF/jsp/common/loadDiv.jsp" %>

					<script>
						$(function () {
							//alert(test);
							console.log('loginname' + PUser.loginname);
							loadCombotree();
							bindtreegrid();
						});

					</script>
					<script>
						function editrow() {
							var node = $('#tt').treegrid('getSelected');
							if (node) {
								//修改
								$("#t1").val(node.deptid);
								if (node.deptid == PUser.deptid) {
									$.messager.alert('消息', '不可编辑本单位');
									return;
								}
								$('#txtBM').val(node.dwbm);
								//$('#t1').attr("disabled", "disabled");
								$('#t2').val(unescape(node.dwmc));
								$('#t3').combotree('setValue', node.parentid);
								$('#t3').combotree('enable');
								//if (node.DISABLED == "1")
								//$('#t4').attr('checked', true);
								//else
								//$('#t4').attr('checked', false);
								//alert(node.XSSX);
								$('#t5').numberbox('setValue', node.xssx);
								//alert(unescape(node.JGSX));
								$('#tJGSX').val(unescape(node.jgsx));
								$('#add').window('open');
							} else
								$.messager.alert('消息', '请选中一个机构');
						}
						function save() {
							if (!$('#ssec-form').form('validate')) {
								return;
							}
							var node = $('#tt').treegrid('getSelected');
							var a = $('#t1').val();
							var b = $('#t2').val();
							var c = $('#t3').combotree('getValue');
							console.log('pid:' + c);
							var d = null;
							var e = $('#t5').val();
							var f = $('#txtBM').val();
							var jgsx = $('#tJGSX').val();
							console.log('loginname' + PUser.loginname);
							$.ajax({
								type: "post",
								url: "DeptManage/AddDept",
								//                dataType: "xml",
								data: {
									deptid: a,
									dwmc: b,
									pid: c,
									dwbm: f,
									xssx: e,
									jgsx: jgsx,
									userid: PUser.loginname
								},
								success: function (data) {
									$.messager.alert('消息', data);
									if (data == '保存成功!') {
										$('#add').window('close');
										bindtreegrid();
									}
								},
								error: function (XMLHttpRequest, textStatus, errorThrown) {
									$.messager.alert('消息', errorThrown);
								}
							});
						}
						function del(node) {
							$.ajax({
								type: "post",
								url: "DeptManage/DelDept",
								//                dataType: "xml",
								data: {
									deptid: node.deptid,
									userid: PUser.loginname
								},
								success: function (data) {
									$.messager.alert('消息', data);
									$('#add').window('close');
									bindtreegrid();
								},
								error: function (XMLHttpRequest, textStatus, errorThrown) {
									$.messager.alert('消息', errorThrown);
								}
							});
						}
					</script>
					<script type="text/javascript">
						function loadCombotree() {
							console.log(PUser);
							$('#t3').combotree({
								url: 'DeptManage/DeptListJson_combobox?deptid=' + PUser.deptid
							});
						}

						function bindtreegrid() {
							$('#tt')
								.treegrid(
									{
										title: '组织机构列表',
										url: 'DeptManage/DeptListJson?deptid='
											+ PUser.deptid,
										iconCls: 'icon-applicationgo',
										nowrap: true,
										width: 1000,
										height: 400,
										rownumbers: false,
										animate: false,
										collapsible: false,
										singleSelect: true,
										remoteSort: false,
										idField: 'deptid',
										treeField: 'dwmc',
										columns: [[{
											field: 'deptid',
											title: 'id',
											formatter: function (val, rec) {
												return unescape(val)
											}
										}, {
											field: 'dwbm',
											title: '机构编码',
											width: 100,
											formatter: function (val, rec) {
												return unescape(val)
											}
										}, {
											field: 'dwmc',
											title: '名称',
											sortable: true,
											width: 200,
											formatter: function (val, rec) {
												return unescape(val)
											}
										}, {
											field: 'xssx',
											title: '排序',
											sortable: true,
											width: 100,
											formatter: function (val, rec) {
												return unescape(val)
											}
										}, {
											field: 'jgsx',
											title: '机构属性',
											sortable: true,
											width: 300,
											formatter: function (val, rec) {
												return unescape(val)
											}
										}]],
										toolbar: [
											{
												text: '新增同级机构',
												//disabled: Permission.indexOf('显示') > 0 ? false : true,
												iconCls: 'icon-add',
												handler: function () {
													$('#ssec-form').form('clear');
													var node = $('#tt').treegrid(
														'getSelected');
													if (node != null) {
														//弹出窗口
														if (node.deptid == PUser.deptid) {
															$.messager.alert('消息',
																"不可以在本级增加同级机构");
															return;
														}
														$('#t3').combotree(
															'setValue',
															node.parentid);
													}
													$('#add').window('open');
												}
											},
											'-',
											{
												text: '增加下级机构',
												iconCls: 'icon-add',

												handler: function () {
													$('#ssec-form').form('clear');
													var node = $('#tt').treegrid(
														'getSelected');
													if (node != null) {
														//弹出窗口
														$('#t3').combotree(
															'setValue',
															node.deptid);
													}
													$('#add').window('open');
												}
											},
											'-',
											{
												text: '修改机构',

												iconCls: 'icon-edit',
												handler: function () {
													editrow();
												}
											},
											'-',
											{
												text: '删除机构',
												iconCls: 'icon-remove',
												handler: function () {
													var node = $('#tt').treegrid(
														'getSelected');
													if (node.deptid == PUser.deptid) {
														$.messager.alert('消息',
															'不可以删除此机构');
														return;
													}

													var nodes;
													if (node) {
														nodes = $('#tt').treegrid(
															'getChildren',
															node.deptid);
														if (nodes.length > 0) {
															$.messager.alert('消息',
																'还有下级机构，不可删除');
														} else {
															$.messager
																.confirm(
																	'提示',
																	'确定要删除【'
																	+ unescape(node.dwmc)
																	+ '】这个机构吗?',
																	function (
																		r) {
																		if (r) {
																			del(node);
																		}
																	});
														}
													} else
														$.messager.alert('消息',
															'请选中一个机构');
												}
											}],
										onLoadSuccess: function () {
											$('#tt').treegrid('collapseAll');
											var rows = $('#tt').treegrid('getChildren');
											if (rows.length) {
												$('#tt').treegrid('expand',
													rows[0].deptid);
											}
										}
									});
						}
					</script>

					<style scoped="scoped">
						.textbox {
							height: 20px;
							margin: 0;
							padding: 0 2px;
							box-sizing: content-box;
						}

						.table tr {
							height: 30px;
						}
					</style>

					<div class="query_criteria" id="query_criteria">
						<table id="tt" style="height: 500px;"></table>
						<div id="add" class="easyui-window" closed="true" modal="true" maximized="false" title="组织机构"
							style="width: 500px; height: 400px; padding: 10px 20px">
							<form id="ssec-form" class="ssec-form">
								<table class="table">
									<tr>
										<td>
											<div>
												机构编码：<input id="t1" type="hidden" />
											</div>
										</td>
										<td>
											<div>
												<input id="txtBM" class="easyui-validatebox textbox" />
											</div>
										</td>
									</tr>
									<tr>
										<td>
											<div class="ssec-label">机构名称：</div>
										</td>
										<td>
											<div class="ssec-text normal">
												<input id="t2" class="easyui-validatebox textbox"
													data-options="required:true" />
											</div>
										</td>
									</tr>
									<tr>
										<td>
											<div class="ssec-label">上级机构：</div>
										</td>
										<td>
											<div>
												<input id="t3" class="easyui-combotree" data-options="required:true"
													style="width: 230px" />
											</div>
										</td>
									</tr>
									<tr>
										<td>
											<div class="ssec-label">排序：</div>
										</td>
										<td>
											<div class="ssec-text normal">
												<input id="t5" class="easyui-numberbox" style="width: 50px;"
													data-options="required:true" />
											</div>
										</td>
									</tr>
									<tr>
										<td>
											<div class="ssec-label">机构属性：</div>
										</td>
										<td>
											<div class="ssec-text normal">
												<input id="tJGSX" class="easyui-validatebox textbox"
													style="width: 260px;" />
											</div>
										</td>
									</tr>
									<tr>
										<td>
											<div class="ssec-label">所属公司：</div>
										</td>
										<td>
											<div class="ssec-text normal">
												<input id="tcorp" class="easyui-combotree" style="width: 230px" />
											</div>
										</td>
									</tr>
								</table>
								<div style="padding: 5px; text-align: right;">
									<a class="easyui-linkbutton" iconcls="icon-save" onclick="save();">保存</a>
								</div>
							</form>
						</div>
					</div>
		</body>

		</html>